let CopyWebpackPlugin = require("copy-webpack-plugin");
let ZipPlugin = require("zip-webpack-plugin");
let path = require("path");
let pageInfo = require("./CubeModule.json");
let webpack = require('webpack');
const vConsolePlugin = require('vconsole-webpack-plugin'); // 引入 移动端模拟开发者工具 插件 （另：https://github.com/liriliri/eruda）
const CompressionPlugin = require('compression-webpack-plugin'); // Gzip
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // Webpack包文件分析器

function checkTime(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function getZipName(type) {
  let d = new Date();
  let year = d.getFullYear();
  let month = checkTime(d.getMonth() + 1);
  let day = checkTime(d.getDate());
  let hour = checkTime(d.getHours());
  let minute = checkTime(d.getMinutes());
  let modes = ['', '-test', '-pro', '-sit']
  return pageInfo.name + '-' + pageInfo.version + modes[type] + '-' + year + month + day + hour + minute + '.zip';
}

const config = {
  // 本地打包配置
  development: {
    devServer: {
      open: true,
      host: '0.0.0.0',
      port: 8088,
      https: false,
      //以上的ip和端口是我们本机的;下面为需要跨域的
      proxy: { // 配置跨域
        '/kitchen': {
           target: 'http://10.187.38.152:8091/', // 这里后台的地址模拟的;应该填写你们真实的后台接口
           ws: true,
           changOrigin: true,// 允许跨域
           pathRewrite: {
             '^kitchen': ''// 请求的时候使用这个api就可以
           }
        }
      }
    },
    configureWebpack: config => {
      //开发环境
      let pluginsDev = [
        //移动端模拟开发者工具(https://github.com/diamont1001/vconsole-webpack-plugin  https://github.com/Tencent/vConsole)
        new vConsolePlugin({
          filter: [], // 需要过滤的入口文件
          enable: false // 发布代码前记得改回 false
        }),
      ];
      config.plugins = [...config.plugins, ...pluginsDev];
    }
  },
  // 测试打包配置
  test: {
    baseUrl: './',
    assetsDir: 'static',
    css: {
      modules: true,
      extract: true,
      sourceMap: true
    },
    configureWebpack: config => {
    /*把dist的文件进行打压缩包处理*/
      //生产and测试环境
      let pluginsTest = [
        new webpack.ProvidePlugin({
          ENVConfig: path.join(__dirname, './src/config/test.config')
        }),
        // 复制 CubeModule.json
        new CopyWebpackPlugin([
          { from: path.join(__dirname, './CubeModule.json'), to: path.join(__dirname, './dist/CubeModule.json') },
        ]),
        // 打压缩包
        new ZipPlugin({
          path: path.join(__dirname, './'),
          filename: getZipName(1)
        }),
        new CompressionPlugin({ //文件开启Gzip，也可以通过服务端(如：nginx)(https://github.com/webpack-contrib/compression-webpack-plugin)
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$', ),
          threshold: 8192,
          minRatio: 0.8,
        }),
        //	Webpack包文件分析器(https://github.com/webpack-contrib/webpack-bundle-analyzer)
        new BundleAnalyzerPlugin(),
      ]
      config.plugins = [...config.plugins, ...pluginsTest]
    },
    chainWebpack: (webpackConfig) => {
    /*设置静态文件夹路径*/
    webpackConfig.resolve
    .alias
    .set('assets',  path.join(__dirname, './src/assets'));
    /*执行buildTest打包时添加webpack一些处理*/
    webpackConfig
    .mode('production')  // buildTest 用webpack 的 production 模式打包
    .devtool('source-map') // 添加 source-map
    .output
    .filename('static/js/[name].[contenthash:8].js') // 输出文件名字及位置
    .chunkFilename('static/js/[name].[contenthash:8].js');
    
    // keep module.id stable when vendor modules does not change
    // 模块代码没修改对应模块文件hash缓存
    webpackConfig
    .plugin('hash-module-ids')
    .use(require('webpack/lib/HashedModuleIdsPlugin'), [{
      hashDigest: 'hex'
    }])
    // 压缩css
    .use(require('@intervolga/optimize-cssnano-plugin'), [{
      sourceMap: true,
      preset: ['default', {
      mergeLonghand: false,
      cssDeclarationSorter: false
      }]
    }])
    }
  },
  // 生产打包配置
  production: {
    baseUrl: './',
    assetsDir: 'static',
    configureWebpack: config => {
      /* 把dist的文件进行打压缩包处理*/
      let pluginsPro = [
        new webpack.ProvidePlugin({
        ENVConfig:  path.join(__dirname, './src/config/prod.config')
        }),
        // 复制 CubeModule.json
        new CopyWebpackPlugin([
        { from: path.join(__dirname, './CubeModule.json'), to: path.join(__dirname, './dist/CubeModule.json') },
        ]),
        // 打压缩包
        new ZipPlugin({
        path: path.join(__dirname, './'),
        filename: getZipName(2)
        }),
        new CompressionPlugin({ //文件开启Gzip，也可以通过服务端(如：nginx)(https://github.com/webpack-contrib/compression-webpack-plugin)
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$', ),
          threshold: 8192,
          minRatio: 0.8,
        }),
        //	Webpack包文件分析器(https://github.com/webpack-contrib/webpack-bundle-analyzer)
        new BundleAnalyzerPlugin()
      ]  
      config.plugins = [...config.plugins, ...pluginsPro]
    },
    chainWebpack: (webpackConfig) => {
        /*设置静态文件夹路径*/
        webpackConfig.resolve.alias.set('assets',  path.join(__dirname, './src/assets'))
    }
  }
}
/*
* npm run serve 命令process.env.NODE_ENV会赋值为development
* npm run buildTest 命令process.env.NODE_ENV会赋值为test
* npm run build 命令process.env.NODE_ENV会赋值为production
* */
module.exports = config[process.env.NODE_ENV];
